﻿﻿using System;
using System.Text;
using GSE.Network;
using System.Threading.Tasks;
using NLog;
using GSE;
using System.Threading;

namespace GSE.test
{
    public class UDPNetTest
    {
        private static Logger logger;
      
        static void Main(string[] args)
        {
            
            var client = new Network.UDP.UDPTransport(args[0]);
            logger = new GSELogger();
            client.SetLogger(logger);
            client.Start();
            client.KeepAlive();
            var n = client.SendData(Encoding.UTF8.GetBytes("哈哈哈"));
            var readTask = Task.Run(() => readAndDisplayResponse(client));
            Task.Run(() => bench(client));
            Task.Run(() => bench(client));
            Console.WriteLine(n);

            n = client.SendDataWithAck(Encoding.UTF8.GetBytes("哦哦哦"));

            Console.WriteLine(n);
            Console.ReadKey();
            client.Close();        
        }

        public static void readAndDisplayResponse(Network.UDP.UDPTransport client)
        {
            while (true)
            {
                var data = client.ReadData();
                logger.Debug($"got server message: {Encoding.UTF8.GetString(data)}");
            }
        }

        public static void bench(Network.UDP.UDPTransport client)
        {
            var rn = new Random(100);
            while (true)
            {
                var data = Encoding.UTF8.GetBytes("哈".PadRight(rn.Next(1000, 2046), 'M'));
                client.SendData(data);
                logger.Debug($"Sent data({data.Length}): {Encoding.UTF8.GetString(data)}");
                //Thread.Sleep(100);
            }
        }
    }

    public class MLogger : NLog.Logger, GSE.Logger
    {
    }
}